package com.slz3.business.module.memberManagement.userScoreRecord.steward;

import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.ConvertUtil;
import com.slz3.business.frame.util.ConverterUtils;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.module.memberManagement.memJifen.domain.MemJifen;
import com.slz3.business.module.memberManagement.userScoreRecord.domain.UserScoreRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @FileName: MemMemberSynr
 * @Description:会员意见反馈同步
 */

@Component
@Slf4j
public class UserScoreRecordBackSync {

    /**
     * 会员积分记录同步处理方法
     */
    public static void syncData() {

        try {
            Object obj = Jedis.db0.leftPop(RK.JI_FEN_RECORD);
            Map<String, Object> dataMap = (Map<String, Object>) obj;
            dataMap.remove("createTime");
            System.out.println(obj);
            UserScoreRecord userScoreRecord = ConvertUtil.objToObj(obj, UserScoreRecord.class);


            MemJifen memJifen = new MemJifen();
            memJifen.setUuid(UUID.fromString(userScoreRecord.getId()));// 积分ID
            memJifen.setCreateTime(LocalDateTime.now());//创建时间
            memJifen.setSerJifenOrderNo(userScoreRecord.getOrderId());// 订单号
            memJifen.setSerJifenUserId(UUID.fromString(userScoreRecord.getUserId()));// 用户ID
            memJifen.setSerJifenType(userScoreRecord.getMemJifenJifenTypeFlag());// 积分类型
            memJifen.setSerJifenState(userScoreRecord.getMemJifenJifenStateFlag());//积分状态
            memJifen.setSerMemo(userScoreRecord.getRemark());// 积分备注
            memJifen.setSerJinfenNum(ConverterUtils.toInt(userScoreRecord.getScore(),0));// 积分值

            Services.memJifen.save(memJifen);
            //消费逻辑
            log.info("消费消息:" + obj.toString());
            //处理完删除set集合
            Services.userScoreRecord.save(userScoreRecord);
            RedisClient.redisTemplate.opsForSet().pop(RK.JI_FEN_RECORD + "Set");
        } catch (Exception e) {
            //主数据set 集合
            Set userScore= Jedis.db0.getSet(RK.SAVE_ORDER.concat("Set"));
            Iterator iterator = userScore.iterator();
            //消费逻辑
            log.info("消费消息:" + iterator.toString());
            //遍历set集合
            while(iterator.hasNext()){
                Map<String, Object> dataMap =(Map<String,Object>) iterator.next();
                dataMap.remove("createTime");
                UserScoreRecord userScoreRecord = ConvertUtil.objToObj(dataMap, UserScoreRecord.class);
                Steward.initNullField(userScoreRecord);
                //根据map先获取订单信息并保存订单


                MemJifen memJifen = new MemJifen();
                memJifen.setUuid(UUID.fromString(userScoreRecord.getId()));// 积分ID
                memJifen.setCreateTime(LocalDateTime.now());//创建时间
                memJifen.setSerJifenOrderNo(userScoreRecord.getOrderId());// 订单号
                memJifen.setSerJifenUserId(UUID.fromString(userScoreRecord.getUserId()));// 用户ID
                memJifen.setSerJifenType(userScoreRecord.getMemJifenJifenTypeFlag());// 积分类型
                memJifen.setSerJifenState(userScoreRecord.getMemJifenJifenStateFlag());//积分状态
                memJifen.setSerMemo(userScoreRecord.getRemark());// 积分备注
                memJifen.setSerJinfenNum(ConverterUtils.toInt(userScoreRecord.getScore(),0));// 积分值

                Services.memJifen.save(memJifen);

                Services.userScoreRecord.save(userScoreRecord);
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.USER_USERREGIST+"Set");
        }

    }



}